package model.components.links.link;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

public class VirtualLink extends Link
{
	public static final String NAME_PREFIX = "VirtualLink";
	
	public static final String VLAN_TYPE = "VLAN";
	public static final String SONET_TYPE = "SONET";
	public static final String EIGHT0211_TYPE = "802.11";
		
	private int startingDate;
	public int getStartingDate(){ return startingDate; }
	public void setStartingDate( int startingDate ){ this.startingDate = startingDate; }
	
	private int endingDate;
	public int getEndingDate(){ return endingDate; }
	public void setEndingDate( int endingDate ){ this.endingDate = endingDate; }
	
	private int lifeTime;
	public int getLifeTime(){ return lifeTime; }
	public void setLifeTime( int lifeTime ){ this.lifeTime = lifeTime; }
	
	private String type;
	public String getType(){ return type; }
	public void setType( String type ){ this.type = type; }
	
	private LinkedHashMap< PhysicalLink, BandwidthBackupFlag > physicalLinksBandwidthsBackupFlags;
	public LinkedHashMap< PhysicalLink, BandwidthBackupFlag > getPhysicalLinksBandwidthsBackupFlags(){ return physicalLinksBandwidthsBackupFlags; }
	public void setPhysicalLinksBandwidthsBackupFlags( LinkedHashMap< PhysicalLink, BandwidthBackupFlag > physicalLinksBandwidthsBackupFlags ){ this.physicalLinksBandwidthsBackupFlags = physicalLinksBandwidthsBackupFlags; }

	private int bandwidthUnitTimeUnitPrice;
	public int getBandwidthUnitTimeUnitPrice(){ return bandwidthUnitTimeUnitPrice; }
	public void setBandwidthUnitTimeUnitPrice( int bandwidthUnitTimeUnitPrice ){ this.bandwidthUnitTimeUnitPrice = bandwidthUnitTimeUnitPrice; }
	
	public VirtualLink( String networkName, int startingDate, int endingDate, int number, String type, int bandwidth )
	{
		super( networkName, VirtualLink.NAME_PREFIX, number, bandwidth, "/icons/virtualLink.png" );
		
		this.setStartingDate( startingDate );
		this.setEndingDate( endingDate );
		this.setLifeTime( endingDate - startingDate );
		this.setType( type );
		this.setPhysicalLinksBandwidthsBackupFlags( new LinkedHashMap< PhysicalLink, BandwidthBackupFlag >() );
	}
	
	@Override
	public VirtualLink getCopy()
	{
		VirtualLink virtualLink = new VirtualLink( this.getNetworkName(), this.getStartingDate(), this.getEndingDate(), this.getNumber(), this.getType(), this.getBandwidth() );
		virtualLink.setNamePrefix( this.getNamePrefix() );
		virtualLink.setName( this.getName() );
		virtualLink.setFirstNode( this.getFirstNode().getCopy() );
		virtualLink.setSecondNode( this.getSecondNode().getCopy() );
		virtualLink.setTimeStamp( this.getTimeStamp() );
		virtualLink.setSelected( this.isSelected() );
		virtualLink.setBandwidthUnitTimeUnitPrice( this.getBandwidthUnitTimeUnitPrice() );
		virtualLink.getPhysicalLinksBandwidthsBackupFlags().putAll( this.getPhysicalLinksBandwidthsBackupFlags() );
				
		return virtualLink;
	}
	
	public void put( PhysicalLink physicalLink, int bandwidth, int flagBackup )
	{
		this.getPhysicalLinksBandwidthsBackupFlags().put( physicalLink, new BandwidthBackupFlag( bandwidth, flagBackup ) );
	}
	
	public void remove( PhysicalLink removePhysicalLink )
	{
		Iterator< Entry< PhysicalLink, BandwidthBackupFlag > > physicalLinksBandwidthsBackupFlagsIterator = this.getPhysicalLinksBandwidthsBackupFlags().entrySet().iterator();
		PhysicalLink physicalLink = null;
		
		while( physicalLinksBandwidthsBackupFlagsIterator.hasNext() )
		{
			physicalLink = physicalLinksBandwidthsBackupFlagsIterator.next().getKey();
			 
			 if( removePhysicalLink.equals( physicalLink ) )
				 break;
		}
		
		this.getPhysicalLinksBandwidthsBackupFlags().remove( physicalLink );
	}

}
